package com.cooper.controllers.seller;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.controllers.LoginRequired;
import com.cooper.core.JsonResult;
import com.cooper.domain.UserSession;
import com.cooper.entity.DesignerApplyDo;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.UserInfo;
import com.cooper.service.admin.AddressService;
import com.cooper.service.admin.LocationService;
import com.cooper.service.core.UserService;
import com.cooper.service.seller.DesignerApplyService;
import com.cooper.service.seller.DesignerService;
import com.cooper.util.BeanUtils;
import com.cooper.util.Controllers;
import com.cooper.vo.AddressVo;
import com.cooper.vo.LocationVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;


@LoginRequired
public class StoreController extends Controller{

	@Autowired
	LocationService locationService;
	
	@Autowired 
	DesignerApplyService designerApplyService;
	
	@Autowired
	AddressService addressService;
	
	@Autowired
	DesignerService designerService;
	
	@Autowired
	private UserService userService;
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Get
	public String execute(Invocation inv) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		DesignerEntity designer = designerService.findDesignerByUserId(userInfo.getId());
		inv.addModel("designer", designer);
		List<LocationVo> firstLevelLocationList = this.locationService.getAllLocationWithChilds(LANG);
		inv.addModel("firstLevelLocationList", firstLevelLocationList);
		if(designer!=null){
			AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
			inv.addModel("address", addressVo);
			inv.addModel("editType", "designer");
		}else{
			DesignerApplyDo applyFail = designerApplyService.findDesignerApplyByUserId(userInfo.getId(),ResourceConstants.DesignerApplyStatus.AUDIT_FAIL);
			if(applyFail!=null){//有失败的申请单话先进入失败的申请单重新提交审核
				AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
				inv.addModel("address", addressVo);
			}
			inv.addModel("designer", applyFail);
			inv.addModel("editType", "designerApply");
			
			return "StoreEdit";
		}
		return "StoreMain";
	}
	@Get("designerApplyDetail")
	public String designerApplyDetail(Invocation inv,@Param("id") Long id) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		DesignerApplyDo designerApply = designerApplyService.findDesignerApplyById(id);
		inv.addModel("designer", designerApply);
		if(designerApply!=null){
			AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
			inv.addModel("address", addressVo);
		}
		return "StoreMain";
	}
	
	@Get("edit")
	public String edit(Invocation inv) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		DesignerEntity designer = designerService.findDesignerByUserId(userInfo.getId());
		if(designer!=null){//已经有店铺信息证明已经审核通过，这里只是修改一些基本信息
			AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
			inv.addModel("address", addressVo);
			inv.addModel("designer", designer);
			inv.addModel("editType", "designer");
		}else{
			DesignerApplyDo applyFail = designerApplyService.findDesignerApplyByUserId(userInfo.getId(),ResourceConstants.DesignerApplyStatus.AUDIT_FAIL);
			if(applyFail!=null){//有失败的申请单话先进入失败的申请单重新提交审核
				AddressVo addressVo = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
				inv.addModel("address", addressVo);
			}
			inv.addModel("editType", "designerApply");
		}
		
		List<LocationVo> firstLevelLocationList = this.locationService.getAllLocationWithChilds(LANG);
		inv.addModel("firstLevelLocationList", firstLevelLocationList);
		return "StoreEdit";
	}
	
	@Post("saveApply")
	public Object saveApply(Invocation inv,DesignerApplyDo designerApplyDo,AddressVo addressVo) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		JsonResult json = new JsonResult();
		try{
			if(designerApplyDo!=null){
				addressVo.setOrigin(ResourceConstants.AddressOrigin.SELLER_ADDRESS);
				
				if(designerApplyDo.getId()!=null){
					DesignerApplyDo designerApply = designerApplyService.findDesignerApplyById(designerApplyDo.getId());
					addressVo.setOuterId(userInfo.getId());
					BeanUtils.copyProperties(designerApplyDo, designerApply);
					designerApply.setUserId(userInfo.getId());
					designerApply.setStatus(ResourceConstants.DesignerApplyStatus.AUDIT);
					designerApplyService.updateDesigner(designerApply);
					addressService.saveAddress(addressVo);
				}else{
					designerApplyDo.setUserId(userInfo.getId());
					designerApplyDo.setStatus(ResourceConstants.DesignerApplyStatus.AUDIT);
					Long designerApplyId = designerApplyService.createDesignerApply(designerApplyDo);
					if(designerApplyId!=null){
						addressVo.setOuterId(userInfo.getId());
						addressService.saveAddress(addressVo);
					}
				}
				
				this.userService.updateUserFileId(userInfo.getId(), designerApplyDo.getSellerFileId());
				UserSession userSession = Controllers.getUserSession(inv);
				userSession.setDesignerPhoto(designerApplyDo.getSellerFileId());
				Controllers.setUserSession(inv, userSession);
			}
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setMessage(e.getMessage());
			json.setSuccess(false);
		}
		return json;
	}
	
	@Post("auditApply")
	public Object auditApply(Invocation inv,@Param("id") Long id,@Param("auditStatus") String auditStatus,@Param("comments") String comments) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		JsonResult json = new JsonResult();
		try{
			DesignerApplyDo designerApplyDo = designerApplyService.findDesignerApplyById(id);
			if(designerApplyDo!=null){
				designerApplyDo.setStatus(auditStatus);
				designerApplyDo.setComments(comments);
				designerApplyService.updateDesigner(designerApplyDo);
				if("audit_success".equals(auditStatus)){
					DesignerEntity designer = new DesignerEntity();
					BeanUtils.copyProperties(designerApplyDo, designer);
					designer.setId(null);
					designer.setGmtCreate(new Timestamp(System.currentTimeMillis()));
					designer.setGmtModified(new Timestamp(System.currentTimeMillis()));
					designer.setCreator(userInfo.getUserName());
					designer.setModifier(userInfo.getUserName());
					designerService.createDesigner(designer);
					UserInfo user = userService.loadById(designer.getUserId());
					user.setUserType(ResourceConstants.UserType.SELLER);
					userService.updateUser(user);
				}
			}
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setMessage(e.getMessage());
			json.setSuccess(false);
		}
		return json;
	}
	
	@Post("save")
	public Object save(Invocation inv,DesignerEntity designerDo,AddressVo addressVo) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		JsonResult json = new JsonResult();
		try{
			if(designerDo!=null){
				addressVo.setOrigin(ResourceConstants.AddressOrigin.SELLER_ADDRESS);
				String fileId = designerDo.getSellerFileId();
				if(StringUtils.isNotEmpty(fileId)){
					fileId = fileId.substring(fileId.indexOf("/")+1);
				}
				if(designerDo.getId()!=null){
					DesignerEntity designer = designerService.findDesignerById(designerDo.getId());
					addressVo.setOuterId(userInfo.getId());
					BeanUtils.copyProperties(designerDo, designer);
					designer.setSellerFileId(fileId);
					designer.setUserId(userInfo.getId());
					designerService.updateDesigner(designer);
					addressService.saveAddress(addressVo);
				}else{
					designerDo.setSellerFileId(fileId);
					designerDo.setUserId(userInfo.getId());
					Long designerId = designerService.createDesigner(designerDo);
					if(designerId!=null){
						addressVo.setOuterId(userInfo.getId());
						addressService.saveAddress(addressVo);
					}
				}
				
				this.userService.updateUserFileId(userInfo.getId(), fileId);
				UserSession userSession = Controllers.getUserSession(inv);
				userSession.setDesignerPhoto(fileId);
				Controllers.setUserSession(inv, userSession);
			}
			json.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			json.setMessage(e.getMessage());
			json.setSuccess(false);
		}
		return json;
	}
	
	@Post("checkIfAudit")
	public Object checkIfAudit(Invocation inv) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		JsonResult json = new JsonResult();
		try{
			DesignerApplyDo apply = designerApplyService.findDesignerApplyByUserId(userInfo.getId(), ResourceConstants.DesignerApplyStatus.AUDIT);
			if(apply!=null){
				json.setSuccess(true);
				json.setMessage("audit");
			}else{
				DesignerEntity designer = designerService.findDesignerByUserId(userInfo.getId());
				if(designer!=null){
					json.setSuccess(true);
				}else{
					json.setSuccess(false);
				}
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			json.setMessage(e.getMessage());
			json.setSuccess(false);
		}
		return json;
	}
}
